<?php

namespace app\canteen\admin;

use app\common\builder\ZBuilder;
use app\common\builder\button\Custom;
use app\common\model\StReservation as StReservationModel;
use app\common\model\StUser as StUserModel;

class StReservation extends Base
{
    public $status=[0=>'取消', 1=>'正常'];
    public function index()
    {
        $map = $this->getMap();
        if (UID > 1) $map =['comid'=>COMID,'delete_time'=>['>',0]];
        $list = (new StReservationModel())->getReservaList($map);
        $page = $list->render();
        return ZBuilder::make('table')
            ->addColumns([
                ['user_name', '用户姓名', 'text'],
                ['date', '用餐时间', 'text'],
                ['comid', '单位', 'text'],
                ['reservation_attr', '用餐详情','text'],
                ['status', '状态', 'status','',$this->status],
               // ['right_button', '操作', 'btn']
            ])
            //批量添加顶部按钮
           // ->addTopButtons('add,enable')
            ->addTopButton('custom', Custom::button('excel'))
            ->addTimeFilter('date') // 添加时间段筛选
            // 批量添加右侧按钮
            ->addRightButtons('edit')
            ->addRightButton('Btn', Custom::button('details','right'),true)//详情按钮
            //设置排序
            ->addOrder('id,date')
            // 设置搜索参数
            ->setSearch(['user_name' => '名称','id'=>'ID号'])
            //设置筛选
            ->addFilterList('status', $this->status)
            ->setTableName('st_reservation')
            ->setRowList($list)
            ->setPages($page)
            ->fetch();
    }

    public function details($id = null){
        if ($id === null) $this->error('缺少参数');

        return $this->fetch();
    }

    /**
     * 导出excel
     */
    public function excel($ids=null){
        if ($ids === null) $this->error('缺少参数');
        $map = $this->getMap();
        $map[] =['id','in',$ids];
        $data_list = (new StReservationModel())->getReservaList($map);
       // dump($data_list);exit();
        // 设置表头信息（对应字段名,宽度，显示表头名称）
        $cellName = [
            ['user_name', 'auto', '用户名'],
            ['date', 'auto', '用餐日期'],
            ['reservation_attr', 'auto', '用餐详情'],
        ];
        // 调用插件（传入插件名，[导出文件名、表头信息、具体数据]）
        plugin_action('Excel/Excel/export', ['预约表', $cellName, $data_list]);
    }

}